package sk.tonyb.library.file;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

/**
 * Class, which provides reading file. <br> 
 * When class will be initialized, content of file will be read. <br> 
 * This content is stored and method getFileContent() return content of this file. <br>  
 * When content of file is changed on disc, it is possible to update content of file in this class. <br>
 * 
 * @author Anton Balucha
 * @since 03.05.2011
 * @last_modified 07.10.2011
 */
public class FileReader {

	/** Log4j logger. */
	public static Logger logger = Logger.getLogger(FileReader.class);
	
	/** Method, which store content of file. */
	private String fileContent = null;
	
	/** Name of file into which will be read. */
	private String fileName = null;

	/** 
	 * Constructor.
	 * 
	 * @param fileName - name of file, from which will be reading. 
	 */
	public FileReader(String fileName) {
		
		if(fileName == null || fileName.trim().isEmpty()) {
			throw new NullPointerException("Entered variable fileName is null or empty.");
		}

		this.fileName = fileName;
		this.readFile();
	}
	
	/** 
	 * Method, which return content of file. <br> 
	 * 
	 * @return fileContent - content of file.
	 */
	public synchronized String getFileContent() {
		return this.fileContent;
	}
	
	/** 
	 * Method, which return name of file. <br>
	 * 
	 * @return fileName - name of file. 
	 */
	public synchronized String getFileName() {
		return this.fileName;
	}
	
	/** 
	 * Method, which update file content. After this update, method return this content. <br>
	 *
	 * @return fileContent - updated file content. 
	 */
	public synchronized String updateFileContent() {
		this.readFile();
		return this.getFileContent();
	}

	/** Method, which provides reading file. After this reading, content of file will be stored. */
	private synchronized void readFile() {
		
		FileInputStream fileInputStream = null;
		DataInputStream dataInputStream = null;
		InputStreamReader inputStreamReader = null;
		BufferedReader bufferedReader = null;
		
		StringBuilder stringBuilder = null;
		String line = null;

		try {

			fileInputStream = new FileInputStream(this.fileName);
			dataInputStream = new DataInputStream(fileInputStream);
			inputStreamReader = new InputStreamReader(dataInputStream);
			bufferedReader = new BufferedReader(inputStreamReader);

			stringBuilder = new StringBuilder();
			
			while ((line = bufferedReader.readLine()) != null) {
				stringBuilder.append(line + "\n");
			}

			bufferedReader.close();
			inputStreamReader.close();
			dataInputStream.close();
			fileInputStream.close();
		}

		catch(FileNotFoundException  e) {
			logger.error("FileNotFoundException", e);
		}

		catch(IOException e) {
			logger.error("IOException", e);
		}
		
		catch (Exception e) {
			logger.error("Exception", e);
		}

		this.fileContent = stringBuilder.toString();
	}
}
